<?php
/*
 * @project CreativeFramework
 * @version <#RELEASE_VERSION#>
 * @license GNU Lesser General Public License
 * @author Jimmie Lin
 */

class Driver implements DB_Driver{
	var $drivername = "MySQL";
	var $usequeries = 'mysql';
	var $driverversion = "1.0.0";

	var $querycount = 0;

	function Connect(){
		global $Debug;
		// OK, Connect.
		if(DB_PERSISTENT == true){
			$this->connection = mysql_pconnect(DB_HOST, DB_USER, DB_PASS);
			$Debug->writeLog("Started Persistent MySQL Connection", "Core");
		}
		else{
			$this->connection = mysql_connect(DB_HOST, DB_USER, DB_PASS);
			$Debug->writeLog("Started Normal MySQL Connection", "Core");
		}
		// Get database.
		if(mysql_select_db(DB_NAME) !== false){
			$Debug->writeLog("Selected MySQL Database", "Core");
			return true;
		}
		else{
			throwFatalError("Cannot find database using provided details, check your configuration and try again");
		}
	}
	
	function Query($query){
		global $Debug;
		$this->querycount++;
		if(substr($query, 0, 6) == "SELECT"){ $type = "READ"; $c = "green"; }
		elseif(substr($query, 0, 6) == "INSERT"){ $type = "WRITE"; $c = "red"; }
		elseif(substr($query, 0, 6) == "UPDATE"){ $type = "WRITE/UPD"; $c = "orange"; }
		else{ $type = "Unknown"; $c = "gray"; }
		$Debug->writeLog("Ran <span style='color:{$c};font-weight:bold'>DB {$type} Query</span> #{$this->querycount}: <span class='monospace'>{$query}</span>", "Core");
		return mysql_query($query);
	}
	
	function Close(){
		if($this->connection){
			mysql_close();
		}
	}
	
	function drop_table($table){
		$query = "DROP TABLE `".DB_PREFIX.$table."`";
		if($this->query($query)){
			return true;
		}
		else return false;
	}
	
	function fetch_array($query){
		return mysql_fetch_array($query);
	}
	
	function escape_string($string){
		return mysql_real_escape_string($string);
	}
	
	
	function insert_query($table, $data){
		$query1 = "INSERT INTO `".DB_PREFIX.$table."`(";
		$query2 = "VALUES(";
		foreach($data as $key => $value){
			if(!isset($temp)){
				$query1 .= "`{$key}`";
				if(is_numeric($value)){
					$query2 .= "{$value}";
				}
				else{
					$query2 .= "'{$value}'";
				}
				$temp = 1;
			}
			else{
				$query1 .= ",`{$key}`";
				if(is_numeric($value)){
					$query2 .= ",{$value}";
				}
				else{
					$query2 .= ",'{$value}'";
				}
			}
		}
		$query2 .= ")";
		$query1 .= ")";
		$query = $query1.$query2;
		return $this->query($query);
	}
	
	function update_query($table, $identifier, $data){
		$c = "";
		$query = "";

		foreach($data as $field => $value){
			$query .= $c."`".$field."`= '{$value}'";
			$c = ", ";
		}

		$idkey = $identifier[0];
		$idvalue = $identifier[1];
		$query .= " WHERE `{$idkey}` = '{$idvalue}'";
		// Now build it
		$query = "UPDATE `".DB_PREFIX."{$table}` SET {$query}";

		return $this->query($query);
	}
	
	function delete_query($table, $identifier){
		$idkey = $identifier[0];
		$idvalue = $identifier[1];
		$query = "WHERE `{$idkey}` = '{$idvalue}'";
		
		return $this->query("DELETE FROM `".DB_PREFIX."{$table}` {$query}");
	}
		
	function num_fields($query){
		return mysql_num_fields($query);
	}
	
	function num_rows($query){
		return mysql_num_rows($query);
	}
	
	function table_exists($table){
		if(DB_CACHE) {
			$cache = " SQL_CACHE";
		}
		else { $cache = ""; }
		$query = "SELECT{$cache} * FROM `".DB_PREFIX.$table;
		if(!$this->query($query)){
			return false;
		}
		else return true;
	}
	
	function simple_select($table, $fields = "*", $conditions = "", $options = array()){
		global $Debug;
		if(DB_CACHE) {
			$cache = " SQL_CACHE";
		}
		else { $cache = ""; }
		$query = "SELECT{$cache} {$fields} FROM `".DB_PREFIX.$table."`";
		if($conditions != "") {
			$query .= " WHERE {$conditions}";
		}
		if(count($options) > 0) {
			foreach($options as $x){
				if($x[0] == "order"){
					$order = " ORDER BY `{$x[1]}` {$x[2]}";
				}
				elseif($x[0] == "limit"){
					$limit = " LIMIT {$x[1]}";
				}
			}
		}
		if(isset($order)) $query = $query.$order;
		if(isset($limit)) $query = $query.$limit;
		$q = $this->query($query);
		return $q;
	}
}
